/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 业务异常
 *
 * 编码过程中，不是任何时候，都能返回一个确切的值，
 *
 * 需要通过抛出 {@link RuntimeException}，强制中断事务，{@link BizException}就是为此设计的。
 * <p>
 * 这是一个特殊的异常类，直接整合了 slf4j 的相关功能，对于 spring 框架来说，不需要特殊处理这个类的日志。
 *
 * 强制：不能用于处理未知的异常，因为不会有任何异常栈信息，设计上也会特地取消相关日志。
 *
 * @author Mr.css on 2017-08-21
 * @author Mr.css on 2020-05-19 重写部分函数，减弱 BizException 的异常捕获能力，再次强调，此异常类不会有详细日志
 */
public class BizException extends RuntimeException {
    private static final long serialVersionUID = 3152511116029346051L;
    private final Logger logger = LoggerFactory.getLogger(BizException.class);
    /**
     * 错误编码
     */
    private int code = 500;
    /**
     * 默认的错误信息
     */
    private String data = "操作失败!";

    public BizException() {
    }

    public BizException(String data) {
        this.data = data;
    }

    public BizException(int code, String data) {
        this.code = code;
        this.data = data;
    }

    public BizException(int code, Throwable cause) {
        this.code = code;
        this.data = cause.getMessage();
        logger.debug("BizException：", cause);
    }

    public BizException(String data, Throwable cause) {
        this.data = data;
        logger.debug(data, cause);
    }

    public BizException(int code, String data, Throwable cause) {
        this.code = code;
        this.data = data;
        logger.debug(data, cause);
    }

    /**
     * 返回错误码
     *
     * @return 错误码
     */
    public int getCode() {
        return code;
    }

    /**
     * 设置错误码
     *
     * @param code -
     */
    public void setCode(int code) {
        this.code = code;
    }

    /**
     * 获取错误信息
     *
     * @return -
     */
    public String getData() {
        return data;
    }

    /**
     * 设置获取错误信息
     */
    public void setData(String data) {
        this.data = data;
    }

    /**
     * 注意，此处输出异常信息栈
     * {@inheritDoc}
     */
    @Override
    public String getMessage() {
        return data;
    }

    /**
     * 注意，此处输出异常信息栈
     * {@inheritDoc}
     */
    @Override
    public void printStackTrace() {
        logger.debug("BizException: " + data);
    }

    /**
     * 注意，此处输出异常信息栈
     * {@inheritDoc}
     */
    @Override
    public void printStackTrace(PrintStream is) {
        is.println(data);
    }

    /**
     * 注意，此处输出异常信息栈
     * {@inheritDoc}
     */
    @Override
    public void printStackTrace(PrintWriter is) {
        is.write(data);
    }
}
